import { FILE_TYPE_OPTIONS, DEFAULT_OPTION } from './constants'

/**
 * 根据文件名称 取后缀名 返回 文件
 * @param fileName
 * @returns {*}
 */
export const getFileOption = (fileName = '') => {
  const suffix = fileName.slice(fileName.lastIndexOf('.') + 1) || ''
  if (suffix) {
    const option = FILE_TYPE_OPTIONS.find(opt => {
      const suffixUppers = opt.suffix.map(e => e.toUpperCase())
      return suffixUppers.includes(suffix.toUpperCase())
    })
    return option || DEFAULT_OPTION
  } else {
    return DEFAULT_OPTION
  }
}

/**
 * 通过获取url中的query中 指定名称的值
 * @param url
 * @param name
 * @returns {string}
 */
export const getQueryValueFromUrl = (url, name) => {
  const regExp = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
  const matchedArray = url.match(regExp)
  let context = ''
  if (matchedArray.length) {
    context = matchedArray[2]
  }
  return [null, undefined, '', 'undefined', 'null']
    .includes(context) ? '' : context
}

